#include "stdafx.h"
#include "MainMenuState.h"
#include "GameplayState.h"
#include "IntroState.h"
#include "TextureManager.h"
#include "BMPText.h"
#include "BitmapTextObject.h"
#include "DirectInput.h"
#include "TalkingDialogueBox.h"

MainMenuState::MainMenuState(void) : m_nCurrentMenuOption(MAIN_MENU_PLAY)
{
	m_pOptions[0] = NULL;
	m_pOptions[1] = NULL;
	m_pOptions[2] = NULL;
	m_pCursor = NULL;
}

MainMenuState::~MainMenuState(void)
{
}


#define CURSOR_X 0
void MainMenuState::Init()
{
	m_nCurrentMenuOption = MAIN_MENU_PLAY;

	//Three bitmap font options
	BitmapTextObject* pObject = new BitmapTextObject();

	pObject->SetText(BIG_FONT, "PLAY", D3DXVECTOR2(100,200));
	m_pOptions[0] = pObject;
	ObjectManager::GetInstance()->AddObject(pObject);

	pObject = new BitmapTextObject();
	pObject->SetText(BIG_FONT, "OPTIONS", D3DXVECTOR2(100,400));
	m_pOptions[1] = pObject;
	ObjectManager::GetInstance()->AddObject(pObject);

	pObject = new BitmapTextObject();
	pObject->SetText(BIG_FONT, "EXIT", D3DXVECTOR2(100,600));
	m_pOptions[2] = pObject;
	ObjectManager::GetInstance()->AddObject(pObject);

	for (int i = 0; i < MAIN_MENU_TOTAL_OPTIONS; ++i)
		SAFE_RELEASE(m_pOptions[i]);

	BaseObject2D* pCursor = new BaseObject2D();
	pCursor->SetTextureID(TextureManager::GetInstance()->LoadTexture("resources\\arrow.png"));

 	ObjectManager::GetInstance()->AddObject(pCursor);
 	pCursor->SetPosition(CURSOR_X,200);
 	pCursor->SetScale(0.35f, 0.35f);
 	m_pCursor = pCursor;
 	SAFE_RELEASE(pCursor);
}

void MainMenuState::Update(float _dt)
{
	if(DirectInput::GetInstance()->KeyPressed(DIK_UP))
	{
		if(m_nCurrentMenuOption == 0)
		{
			m_nCurrentMenuOption = MAIN_MENU_TOTAL_OPTIONS-1;
			m_pCursor->SetPosition(CURSOR_X, 600);
		}
		else
		{
			m_nCurrentMenuOption -= 1;
			m_pCursor->SetPosition(CURSOR_X, m_pCursor->GetPosition().y - 200);
		}
	}
	if(DirectInput::GetInstance()->KeyPressed(DIK_DOWN))
	{
		if(m_nCurrentMenuOption == MAIN_MENU_TOTAL_OPTIONS-1)
		{
			m_nCurrentMenuOption = 0;
			m_pCursor->SetPosition(CURSOR_X, 200);
		}
		else
		{
			m_nCurrentMenuOption += 1;
			m_pCursor->SetPosition(CURSOR_X, m_pCursor->GetPosition().y + 200);
		}
	}

	if(DirectInput::GetInstance()->KeyPressed(DIK_RETURN))
	{
		switch(m_nCurrentMenuOption)
		{
		case MAIN_MENU_PLAY:
			{
				StateManager::GetInstance()->ChangeState(GameplayState::GetInstance());
				break;
			}
		case MAIN_MENU_OPTIONS:
			{
				//Intro for now
				//StateManager::GetInstance()->ChangeState(IntroState::GetInstance());
				break;
			}
		case MAIN_MENU_EXIT:
			{
				StateManager::GetInstance()->Pop();
				break;
			}
		}
	}
}

void MainMenuState::Render()
{
	ObjectManager::GetInstance()->RenderObjects();
}

void MainMenuState::Shutdown()
{
	ObjectManager::GetInstance()->RemoveAllObjects();
}
